package com.uroad.liaoningpos.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.uroad.liaoningpos.R;
import com.uroad.liaoningpos.common.BaseActivity;
import com.uroad.liaoningpos.common.SPContants;
import com.uroad.liaoningpos.util.SPUtils;
import com.uroad.liaoningpos.webService.PosWS;
import com.uroad.lib.json.FastJsonUtils;
import com.uroad.lib.util.encrypt.SecurityUtil;
import com.uroad.lib.widget.ClearEditText;
import com.uroad.lib.widget.dialog.DialogHelper;


import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @创建者 wlw
 * @创建时间 2019/4/3 14:48
 * @描述 修改密码
 */
public class ModifyPwdActivity extends BaseActivity {
    @BindView(R.id.etOldPwd)
    ClearEditText etOldPwd;
    @BindView(R.id.etNewPwd)
    ClearEditText etNewPwd;
    @BindView(R.id.etNewPwdAgain)
    ClearEditText etNewPwdAgain;
    @BindView(R.id.tvErrorTrip)
    TextView tvErrorTrip;
    @BindView(R.id.btnSubmit)
    Button btnSubmit;
    private String oldPwd= "";
    private String newPwd= "";
    private String newPwdAgain= "";
    private String useruuid= "";
    private String merchantshopid= "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentLayout(R.layout.activity_modify_pwd);
        ButterKnife.bind(this);
        initData();
        setTitle("修改密码");
    }

    private void initData() {
        useruuid = (String) SPUtils.get(this, SPContants.SP_USERUUID, "");
        merchantshopid = (String) SPUtils.get(this, SPContants.SP_MERCHANTSHOPID, "");
    }

    @OnClick(R.id.btnSubmit)
    public void onClick(View view) {
        oldPwd = etOldPwd.getText().toString().trim();
        if (TextUtils.isEmpty(oldPwd)) {
            tvErrorTrip.setText("请输入原密码");
            return;
        }
        newPwd = etNewPwd.getText().toString().trim();
        if (TextUtils.isEmpty(newPwd)) {
            tvErrorTrip.setText("请输入新密码");
            return;
        }
        newPwdAgain = etNewPwdAgain.getText().toString().trim();
        if (TextUtils.isEmpty(newPwdAgain)) {
            tvErrorTrip.setText("请再次输入新密码");
            return;
        }
        if (!newPwd.equals(newPwdAgain)) {
            tvErrorTrip.setText("两次新密码输入不一致");
            return;
        }
        if (newPwd.equals(oldPwd)) {
            tvErrorTrip.setText("新密码不能与原密码一致");
            return;
        }
        tvErrorTrip.setText("");
        try {
            oldPwd = SecurityUtil.EncoderByMd5(oldPwd);
            newPwd = SecurityUtil.EncoderByMd5(newPwd);
            modifyPwd();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void modifyPwd() {
        DialogHelper.showLoading(this,"正在修改");
        doRequest(PosWS.updatepassword,PosWS.updatepasswordParams(useruuid,merchantshopid,oldPwd,newPwd),
                PosWS.updatepassword);
    }

    @Override
    public void OnHttpTaskComplete(String result, String id) {
        super.OnHttpTaskComplete(result, id);
        DialogHelper.endLoading();
        JSONObject object = JSON.parseObject(result);
        if (FastJsonUtils.getJsonStatu00(object)) {
            showShortToast("修改成功");
            finish();
        }else{
            showShortToast(FastJsonUtils.getErrMsg00(object));
        }
    }

    @Override
    public void OnHttpFailure(String msg) {
        super.OnHttpFailure(msg);
        DialogHelper.endLoading();
    }

    @Override
    public void OnHttpNetWork(String msg) {
        super.OnHttpNetWork(msg);
        DialogHelper.endLoading();
    }
}
